
#include <QtGui>
#include <QPainter>

#include "show3dimage.h"
#include "fileloader.h"

    Show3DImage::Show3DImage(QWidget * parent, int maxX, int maxY, int maxZ) : QDialog(parent){
        setupUi(this, maxX, maxY, maxZ);
        
        connect(xSlider, SIGNAL(valueChanged(int)), this, SLOT(updateX(int)));
        connect(ySlider, SIGNAL(valueChanged(int)), this, SLOT(updateY(int)));
        connect(zSlider, SIGNAL(valueChanged(int)), this, SLOT(updateZ(int)));
        
        fl = FileLoader::getInstance();
        updateZ(1);
        updateY(1);
        updateX(1);
    }

    void Show3DImage::setupUi(QWidget *mainField, int maxX, int maxY, int maxZ)
    {
        if (mainField->objectName().isEmpty())
            mainField->setObjectName(QString::fromUtf8("mainField"));
            
        int w = maxY;
        if(w < 160) w = 160;
        int h = maxZ;
        if(h < 160) h = 180;
                
        mainField->resize(maxX+w+30, maxZ+h+30);
        
        xLabel = new QLabel(mainField);
        xLabel->setObjectName(QString::fromUtf8("xLabel"));
        xLabel->setGeometry(QRect(20+maxX, 20+maxZ, w, 25));
        xSlider = new QSlider(mainField);
        xSlider->setObjectName(QString::fromUtf8("xSlider"));
        xSlider->setGeometry(QRect(20+maxX, 20+maxZ+30, w, 25));
        xSlider->setOrientation(Qt::Horizontal);
        xSlider->setMinimum(1);
        xSlider->setMaximum(maxX);
        
        yLabel = new QLabel(mainField);
        yLabel->setObjectName(QString::fromUtf8("yLabel"));
        yLabel->setGeometry(QRect(20+maxX, 20+maxZ+60, w, 25));
        ySlider = new QSlider(mainField);
        ySlider->setObjectName(QString::fromUtf8("ySlider"));
        ySlider->setGeometry(QRect(20+maxX, 20+maxZ+90, w, 25));
        ySlider->setOrientation(Qt::Horizontal);
        ySlider->setMinimum(1);
        ySlider->setMaximum(maxY);
        
        zLabel = new QLabel(mainField);
        zLabel->setObjectName(QString::fromUtf8("zLabel"));
        zLabel->setGeometry(QRect(20+maxX, 20+maxZ+120, w, 25));
        zSlider = new QSlider(mainField);
        zSlider->setObjectName(QString::fromUtf8("zSlider"));
        zSlider->setGeometry(QRect(20+maxX, 20+maxZ+150, w, 25));
        zSlider->setOrientation(Qt::Horizontal);
        zSlider->setMinimum(1);
        zSlider->setMaximum(maxZ);
               
        YImage = new QLabel(mainField);
        YImage->setObjectName(QString::fromUtf8("XImage"));
        YImage->setGeometry(QRect(10, 10, maxX, maxZ));
        
        XImage = new QLabel(mainField);
        XImage->setObjectName(QString::fromUtf8("YImage"));
        XImage->setGeometry(QRect(20+maxX, 10, maxY, maxZ));
        
        ZImage = new QLabel(mainField);
        ZImage->setObjectName(QString::fromUtf8("ZImage"));
        ZImage->setGeometry(QRect(10, 20+maxZ, maxX, maxZ));
               
        mainField->setWindowTitle("3D image view");
        
        xLabel->setText("X position: 1");
        zLabel->setText("Z position: 1");
        yLabel->setText("Y position: 1");

        QMetaObject::connectSlotsByName(mainField);
        
        
        
    } // setupUi

    void Show3DImage::updateX(int val){
        xLabel->setText(QString("X position: ")+QString::number(xSlider->value()));
        XImage->setPixmap(QPixmap::fromImage(
             *(fl->getXImage(xSlider->value(), ySlider->value(), zSlider->value())),Qt::ColorOnly));
    }
    void Show3DImage::updateY(int val){
        yLabel->setText(QString("Y position: ")+QString::number(ySlider->value()));
        YImage->setPixmap(QPixmap::fromImage(
             *(fl->getYImage(xSlider->value(), ySlider->value(), zSlider->value())),Qt::ColorOnly));
    }
    void Show3DImage::updateZ(int val){
        zLabel->setText(QString("Z position: ")+QString::number(zSlider->value()));
        ZImage->setPixmap(QPixmap::fromImage(
             *(fl->getZImage(xSlider->value(), ySlider->value(), zSlider->value())),Qt::ColorOnly));
    }
